package org.example;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * Author:qqw
 * Date:2024-06-18
 * Description:转发服务处理器
 */
@Slf4j
public class ForwarderServerHandler extends ChannelInboundHandlerAdapter {

    private ChannelHandlerContext receiveCtx;
    private Object receiveMsg;

    public ForwarderServerHandler(ChannelHandlerContext receiveCtx,Object receiveMsg){
        this.receiveCtx = receiveCtx;
        this.receiveMsg = receiveMsg;
    }

    @Override
    public void channelActive(ChannelHandlerContext forwarderCtx) throws Exception {
        log.info("NettyDispatcher: send msg to target port,msg:{}",DecodeUtil.decode((ByteBuf) receiveMsg));
        forwarderCtx.writeAndFlush(receiveMsg);
    }

    @Override
    public void channelRead(ChannelHandlerContext forwarderCtx, Object msg) throws Exception {
        log.info("NettyDispatcher: receive msg from target port,msg:{}",DecodeUtil.decode((ByteBuf)msg));
        receiveCtx.writeAndFlush(msg);
    }
}
